package com.example.jf.jfapp.activity.base;

import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.jf.jfapp.R;

/**
 * Created by jf on 2017/7/29.
 */

public class BaseMultipleStatuActivity extends  BaseActivity {
    /**
     * 加载状态布局
     */
    LinearLayout ll_loading;
    LinearLayout empty_view;
    LinearLayout error_view;

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        FrameLayout rootView = (FrameLayout) LayoutInflater.from(this).inflate(R.layout.activity_base_multiple_statu, null);
        ll_loading = rootView.findViewById(R.id.loading_view);
        empty_view = rootView.findViewById(R.id.empty_view);
        error_view = rootView.findViewById(R.id.error_view);
        View childView = LayoutInflater.from(this).inflate(layoutResID, rootView, false);
        //如果不加0，childView会覆盖其它View
        rootView.addView(childView, 0);
        setContentView(rootView);
    }


    /**
     * 显示加载布局
     */
    protected void showLoadingView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (ll_loading != null) {
                    ll_loading.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    /**
     * 显示空数据状态布局
     */
    protected void showEmptyView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (error_view != null) {
                    error_view.setVisibility(View.GONE);
                }
                if (empty_view != null) {
                    empty_view.setVisibility(View.VISIBLE);
                }
                if (ll_loading != null) {
                    ll_loading.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 显示错误状态布局
     */
    protected void showErrorView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (error_view != null) {
                    error_view.setVisibility(View.VISIBLE);
                }
                if (empty_view != null) {
                    empty_view.setVisibility(View.GONE);
                }
                if (ll_loading != null) {
                    ll_loading.setVisibility(View.GONE);
                }
            }
        });

    }

    /**
     * 显示正确状态布内容局
     */
    protected void showContentView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (error_view != null) {
                    error_view.setVisibility(View.GONE);
                }
                if (empty_view != null) {
                    empty_view.setVisibility(View.GONE);
                }
                if (ll_loading != null) {
                    ll_loading.setVisibility(View.GONE);
                }
            }
        });
    }
}
